package com.wrx.club_management_backend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wrx.club_management_backend.entity.ClubActivity;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface ClubActivityMapper extends BaseMapper<ClubActivity> {

    /**
     * 添加用户活动参与记录
     *
     * @param userId     用户ID
     * @param activityId 活动ID
     */
    @Insert("INSERT INTO tb_activity_participate(user_id, activity_id, create_time) VALUES(#{userId}, #{activityId}, NOW())")
    void insertActivityParticipant(@Param("userId") Long userId, @Param("activityId") Long activityId);


    @Select("select count(*) >= 1 from tb_activity_participate where user_id = #{userId} and activity_id = #{activityId}")
    boolean selectByUserIdAndActivityId(@Param("userId") Long userId, @Param("activityId") Long activityId);

    @Select("select user_id from tb_activity_participate where activity_id = #{activityId} ")
    List<Long> listUserById(@Param("activityId") Long activityId);

    /**
     * 根据日期范围查询每天的活动参与人数
     *
     * @param startDate 开始日期，格式：yyyy-MM-dd
     * @param endDate   结束日期，格式：yyyy-MM-dd
     * @return 日期和对应的参与人数
     */

    @Select("SELECT DATE(create_time) as date, COUNT(id) as count FROM tb_activity_participate " +

            "WHERE create_time >= STR_TO_DATE(#{startDate}, '%Y-%m-%d') " +

            "AND create_time <= STR_TO_DATE(#{endDate}, '%Y-%m-%d') + INTERVAL 1 DAY " +

            "GROUP BY DATE(create_time) ORDER BY date")
    List<Map<String, Object>> getActivityParticipantsByDateRange(@Param("startDate") String startDate, @Param("endDate") String endDate);

    @Delete("delete from tb_activity_participate where user_id = #{userId}")
    void deleteParticipant(@Param("userId") Long userId);
}
